package com.book.activeMQ.send;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @Author 李锦程
 * @Date 2021/4/21 11:36 上午
 * @Package com.book.activeMQ.send
 * @Version 1.0
 */
@Component
public class SendMessage {
    @Autowired
    private JavaMailSender javaMailSender;

    public void sendMailCode(String subject,String toMail,String text) {
        MimeMessage mMessage = javaMailSender.createMimeMessage();//创建邮件对象
        MimeMessageHelper mMessageHelper;
        Properties prop = new Properties();
        String from;
        try {
            //从配置文件中拿到发件人邮箱地址
            prop.load(this.getClass().getResourceAsStream("/db.properties"));
            from = prop.get("mail.smtp.username") + "";
            mMessageHelper = new MimeMessageHelper(mMessage, true);
            mMessageHelper.setFrom(from);
            mMessageHelper.setTo(toMail);
            mMessageHelper.setSubject(subject);
            mMessageHelper.setText(text, true);
            javaMailSender.send(mMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
